﻿using System.Linq;
using System.Web.Mvc;
using Yonkly.Utility;

namespace Yonkly.Models
{
    public class ConversationViewData : BaseViewData
    {
        private int Page { get; set; }

        public ConversationViewData(int? page)
        {
            Page = page ?? 0;
        }

        public ConversationViewData(BaseViewData mybase, int? page)
            : base(mybase)
        {
            Page = page ?? 0;
        }

        public long Id { get; set; }

        public MessageItem Message
        {
            get
            {
                using (var dc = new YonklyDataContext())
                {
                    if (IsGroup)
                    {
                        var query = from messages in dc.ViewGroupMessages
                                    where messages.MessageId == Id
                                    select new MessageItem(this, messages);
                        return query.First();
                    }
                    else
                    {
                        var query = from messages in dc.ViewMessages
                                    where messages.MessageId == Id
                                    select new MessageItem(this, messages);
                        return query.First();
                    }
                }
            }
        }

        public PagedList<MessageItem> Children
        {
            get
            {
                using (var dc = new YonklyDataContext())
                {
                    if (IsGroup)
                    {
                        var query = (from message in dc.ViewGroupMessages
                                     where message.ParentId == Id
                                     select new MessageItem(this, message) { CreatedOn = message.CreatedOn }).
                        OrderByDescending(
                        m => m.CreatedOn);
                        return query.ToPagedList((int)Page, Globals.PageSize);
                    }
                    else
                    {
                        var query = (from messages in dc.ViewMessages
                                     where messages.ParentId == Id
                                     select new MessageItem(this, messages) { CreatedOn = messages.CreatedOn }).
                        OrderByDescending(
                        m => m.CreatedOn);
                        return query.ToPagedList((int)Page, Globals.PageSize);
                    }

                }
            }
        }
    }

    public class GroupConversationViewData : GroupViewData
    {
        private int Page { get; set; }

        public GroupConversationViewData(BaseViewData mybase, int? page)
            : base(mybase)
        {
            Page = page ?? 0;
        }

        public long Id { get; set; }

        public MessageItem Message
        {
            get
            {
                using (var dc = new YonklyDataContext())
                {
                    var query = from messages in dc.ViewGroupMessages
                                where messages.MessageId == Id
                                select new MessageItem(this, messages);
                    return query.First();
                }
            }
        }

        public PagedList<MessageItem> Children
        {
            get
            {
                using (var dc = new YonklyDataContext())
                {
                    var query = (from message in dc.ViewGroupMessages
                                 where message.ParentId == Id
                                 select new MessageItem(this, message) { CreatedOn = message.CreatedOn }).
                    OrderByDescending(
                    m => m.CreatedOn);
                    return query.ToPagedList((int)Page, Globals.PageSize);
                }
            }
        }
    }
}